package com.hjs.test.jst;

import java.time.LocalDateTime;
import java.time.Month;
import java.time.format.DateTimeFormatter;

public class LocalDateTimeExample {
    public static void main(String[] args) {
        // 获取当前时间
        LocalDateTime now = LocalDateTime.now();
        System.out.println("当前时间: " + now);

        // 根据指定日期/时间创建对象
        LocalDateTime dateTime = LocalDateTime.of(2024, Month.MAY, 11, 9, 17, 9);
        System.out.println("指定时间: " + dateTime);

        // 修改日期时间的某个值
        LocalDateTime modifiedDateTime = dateTime.withHour(14).withMinute(0);
        System.out.println("修改后的时间: " + modifiedDateTime);

        // 获取日期时间的各个部分
        int year = dateTime.getYear();
        Month month = dateTime.getMonth();
        int dayOfMonth = dateTime.getDayOfMonth();
        int hour = dateTime.getHour();
        int minute = dateTime.getMinute();
        int second = dateTime.getSecond();
        System.out.println("年: " + year + ", 月: " + month + ", 日: " + dayOfMonth + ", 时: " + hour + ", 分: " + minute + ", 秒: " + second);

        // 格式转换
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String formattedDateTime = dateTime.format(formatter);
        System.out.println("格式化后的时间: " + formattedDateTime);

        // 解析字符串为LocalDateTime对象
        LocalDateTime parsedDateTime = LocalDateTime.parse("2024-05-11 09:17:09", formatter);
        System.out.println("解析后的时间: " + parsedDateTime);
    }
}
